﻿using Studio77.FruitLinkLink2.Core;
using Studio77.FruitLinkLink2.Pages;
using Studio77.FruitLinkLink2.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace Studio77.FruitLinkLink2
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private static bool isFirstLoad = true;

        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= MainPage_Loaded;

            //事件注册Here
            this.btnEasyGame.Click += btnEasyGame_Click;
            this.btnNormalGame.Click += btnNormalGame_Click;
            this.btnHardGame.Click += btnHardGame_Click;
            this.btnHelp.Click += btnHelp_Click;
            this.btnExit.Click += btnExit_Click;
            this.btnSound.Click += btnSound_Click;
            this.btnSound_Mute.Click += btnSound_Mute_Click;

            //初始化工作Here
            StageManager.Instance.Init(ApplicationData.Current.LocalSettings);

            //播放音乐
            if (isFirstLoad)
            {
                AudioPlayHelper.PlayBackground("music.mp3");
                isFirstLoad = false;
            }

            if (StageManager.Instance.isPlaySound)
            {
                this.btnSound_Mute.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.btnSound.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.btnSound_Mute.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.btnSound.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        void btnExit_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Exit();
        }

        void btnHelp_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(HelpPage));
        }

        void btnHardGame_Click(object sender, RoutedEventArgs e)
        {
            StartNewGame(3);
        }

        void btnNormalGame_Click(object sender, RoutedEventArgs e)
        {
            StartNewGame(2);
        }

        void btnEasyGame_Click(object sender, RoutedEventArgs e)
        {
            StartNewGame(1);
        }

        /// <summary>
        /// 开始游戏
        /// </summary>
        /// <param name="type">1-easy, 2-normal, 3-hard</param>
        private void StartNewGame(int type)
        {
            //跳转到GamePage
            this.Frame.Navigate(typeof(GamePage), type);
        }


        void btnSound_Mute_Click(object sender, RoutedEventArgs e)
        {

            StageManager.Instance.isPlaySound = true;
            StageManager.Instance.SavePlaySoundState();
            AudioPlayHelper.PlayBackground("music.mp3");

            this.btnSound_Mute.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            this.btnSound.Visibility = Windows.UI.Xaml.Visibility.Visible;
        }

        void btnSound_Click(object sender, RoutedEventArgs e)
        {
            StageManager.Instance.isPlaySound = false;
            StageManager.Instance.SavePlaySoundState();
            AudioPlayHelper.StopBackground();

            this.btnSound_Mute.Visibility = Windows.UI.Xaml.Visibility.Visible;
            this.btnSound.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
    }
}
